const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    imgId:[],
    imgIdChange:[],
    shopType:[],
    shopTypeId:""
  },
  // 获取上传商品名称
  shopName:function(res){
    console.log(res)
  },
  // 获取上传商品img
  shopImg:function(res){
    let that = this
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })
        const filePath = res.tempFilePaths[0]
        // 上传图片
        const cloudPath = Math.random().toString(36).substr(2) + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)
            let imgId = that.data.imgId
            imgId.push(res.fileID)
            that.setData({
              imgId: imgId
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      }
    })
  },
  // 上传 内容
  upimg:function(t){
    let that = this
    let detail = t.detail.value.shop-detail
    let name = t.detail.value.shop-name
    db.collection('shopUp').add({
      data: {
        detail: detail,
        name: name,
        upimg: that.data.imgId,
        shopTypeId: that.data.shopTypeId
      },
      success: res => {
        wx.showToast({
          title: '新增记录成功',
        })
        console.log('[数据库] [新增记录] 成功，记录 _id: ', res)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '新增记录失败'
        })
        console.error('[数据库] [新增记录] 失败：', err)
      }
    })
  },
  // 图片预览
  previewImage:function(e){
    console.log(e)
    let that = this
    let index = e.currentTarget.dataset.index 
    let imgId = that.data.imgId
    wx.previewImage({
      current: imgId[index],
      urls: that.data.imgId,
      success: function(res) {
        console.log(res)
      }
    })
  },
  onLoad: function (options) {
    console.log(options.id)
    let that = this
    that.setData({
      _id:options.id
    })
    db.collection('shopUp').where({
      _id: options.id
    }).get({
      success: res => {
        this.setData({
          shopUpChange: res.data
        })
        console.log('[数据库] [查询记录] 成功: ', res)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败：', err)
      }
    })
  },
  shopImgChange: function (res) {
    let that = this
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })
        const filePath = res.tempFilePaths[0]
        // 上传图片
        const cloudPath = Math.random().toString(36).substr(2) + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)
            let imgIdChange = that.data.imgIdChange
            imgIdChange.push(res.fileID)
            that.setData({
              imgIdChange: imgIdChange
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        
      }
    })
  },
  upimgChange:function(e){
    console.log(e.detail.value)
    db.collection('counters').doc(this.data.counterId).update({
      data: {
        count: newCount
      },
      success: res => {
        this.setData({
          count: newCount
        })
      },
      fail: err => {
        icon: 'none',
        console.error('[数据库] [更新记录] 失败：', err)
      }
    })
  },
  onReady: function () {
    let that = this
    db.collection('shopType').where({}).get({
      success: res => {
        console.log('[数据库] [查询记录] 成功: ', res)
        var shoptype = []
        for(let i=0;i<res.data.length;i++){
          console.log(res.data[i].shopType)
          shoptype.push(res.data[i].shopType)          
        }
        that.setData({
          shopType: shoptype,
          shopTypeChoose:res.data
        })
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败：', err)
      }
    })
  },
  shopTypeChoose:function(e){
    console.log(e.detail.value)
    let that = this;
    console.log(that.data.shopTypeChoose[e.detail.value].shopType)
    that.setData({
      shopTypeId: that.data.shopTypeChoose[e.detail.value]._id,
      shopTypeView: that.data.shopTypeChoose[e.detail.value].shopType
    })
  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})